package com.gexin.model.system;
import com.gexin.common.utils.MD5;
import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.system.base.BaseAdminUser;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Page;

/**
 * 业务名称:  t_admin_user(系统管理员 和区域管理员的表) 
 */
@SuppressWarnings("serial")
public class AdminUser extends BaseAdminUser<AdminUser> {
	public static final AdminUser dao = new AdminUser();

	/** 超级管理员 */
	public  static  final  Integer userRole_type0=-1;
	/** 商品管理员 */
	public  static  final  Integer userRole_type11=11;
	/** 商品审核员 */
	public  static  final  Integer userRole_type12=12;

	/**
	 * 用户登录的接口
	 * @param username  用户名字
	 * @param pwd   用户密码
	 * @return
	 */
	public  AdminUser  login(String username ,String pwd){
		AdminUser  member  = getAdminByUsername(username);
		if (member != null && member.getPassword().equals(MD5.MD5EncodePass(pwd)) ) {
			return member;
		} else {
			return null;
		}
	}
	
	/**
	 * 根据用户名查询详细信息
	 * @param username  用户名
	 * @return  用户的信息
	 */
	public AdminUser getAdminByUsername(String username) {
		if(StringUtil.isBlank(username)){
			LogKit.error("用户参数错误！");
			return null;
		}
		//System.err.println(dao.findFirst("select * from t_admin_user where username = 'admin@admin.com'").toJson());
		return dao.findFirst("select * from t_admin_user where username =  ?",username);
	}
	
	
	/**
	 * 分页
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<AdminUser> paginate(int pageNumber, int pageSize,AdminUser admin) {
		SQLUtils sql  = new SQLUtils("from t_admin_user as u  , t_admin_role role where u.role_id=role.id ");
		if(admin!=null){
			if(StringUtil.isEmpty(admin.getUsername())==false){
				sql.append(" and u.username like '%"+admin.getUsername()+"%'");
			}
			
			if(StringUtil.isEmpty(admin.getName())==false){
				sql.append(" and u.name like '%"+admin.getUsername()+"%'");
			}
		}
		sql.append("order by u.id asc");
		return paginate(pageNumber, pageSize, "select u.*,role.name as role_name ", sql.toString());
	}
	/**
	 * 查询用户的详细信息
	 * @param userid
	 * @return
	 */
	public  AdminUser  findByIdUserinfo(Integer  userid){
		//role_name 
		String sql = "select u.*,role.name as role_name  from t_admin_user as u  , t_admin_role role where u.role_id=role.id and u.id=?";
		return dao.findFirst(sql, userid);
	}

}
